﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.IO;
using FirstFloor.Documents.Fonts;

namespace SampleBrowser.Util
{
    /// <summary>
    /// Provides font related helpers.
    /// </summary>
    public static class FontHelper
    {
        /// <summary>
        /// Opens a font stream from disk.
        /// </summary>
        /// <returns></returns>
        public static Stream OpenFontFromDisk()
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "All Fonts (*.ttf,*.otf,*.ttc,*.odttf,*.odotf,*.odttc)|*.ttf;*.otf;*.ttc;*.odttf;*.odotf;*.odttc|Fonts (*.ttf,*.otf,*.ttc)|*.ttf;*.otf;*.ttc|Obfuscated Fonts (*.odttf,*.odotf,*.odttc)|*.odttf;*.odotf;*.odttc|All Files (*.*)|*.*";
            if (dlg.ShowDialog() == true) {
                Stream stream = dlg.File.OpenRead();

                if (!FontParser.IsValidFontStream(stream)) {
                    // assume font is obfuscated
                    var fileName = Path.GetFileName(dlg.File.Name);
                    stream = DeobfuscatedFontStream.Create(stream, fileName);
                }
                return stream;
            }

            return null;
        }
    }
}
